<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Konten extends CI_Controller {
	
	function __construct(){		
		parent::__construct();	
		//$this->load->library('Excel_generator');
		//$this->output->enable_profiler(true);
		
		$this->load->model('mdl_konten');
	}
	
	function index(){
		$data['page_title']='Seting Konten Surat';
		$data['page_icon']='icon-envelope';
		$data['page_content']='daftar_konten';
		
		$this->load->view('layout/master',$data);
	}
	
	
	function grid(){
		$data = $this->mdl_konten->getdata();
		echo $this->mdl_konten->togrid($data['row_data'], $data['row_count']);
	}

	function add(){

		$data['id_konten'] = '';
		$data['jenis_surat'] = '';
		$data['paragraf_1'] = '';
		$data['paragraf_2'] = '';
		$data['paragraf_3'] = '';
		$data['paragraf_4'] = '';
		
		$this->load->view('konten_form', $data);
	}

	function edit($id_konten){
		$r = $this->mdl_konten->getDataEdit($id_konten);

		$data['id_konten'] = $r->row()->id_konten;
		$data['jenis_surat'] = $r->row()->jenis_surat;
		$data['paragraf_1'] = $r->row()->paragraf_1;
		$data['paragraf_2'] = $r->row()->paragraf_2;
		$data['paragraf_3'] = $r->row()->paragraf_3;
		$data['paragraf_4'] = $r->row()->paragraf_4;
		
		$this->load->view('konten_form', $data);
	}

	function save($aksi="add"){
		# init
		$status = "";
		$result = false;
		//$data['pesan_error'] = '';
		$data = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}


		// $data['list_anggota_keluarga'] = json_decode($data['anggota_keluarga']);
		// unset($data['anggota_keluarga']);
		
		# rules validasi form
		$this->form_validation->set_rules("jenis_surat", 'Jenis Surat', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("nama_desa", 'Nama Dusun', 'trim|required|xss_clean');	
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi == 'add'){
				$result = $this->mdl_konten->InsertOnDb($data);
			}else{
				$result = $this->mdl_konten->UpdateOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('success'=>false, 'msg'=>$data['pesan_error']));
		}
		
	}
}